﻿using System.Reflection;
using System.Windows.Forms;
using AutoTAU.Properties;

namespace AutoTAU
{
    /// <summary>
    /// Manages the notification icon and GUI feedback (changing icons and balloon notifications)
    /// </summary>
    internal static class GUINotificationManager
    {
        /// <summary>
        /// Notification icon form
        /// </summary>
        public static NotifyIcon sysTrayIcon;

        /// <summary>
        /// Set the tray icon and balloon tip on Wifi authentication events
        /// </summary>
        /// <param name="status">Wifi status</param>
        /// <param name="title">Message title</param>
        /// <param name="msg">Message body</param>
        /// <param name="isCritical">Is this a critical message</param>
        private static void NotifyWifiAuthEventHandler(WifiStatus status, string title, string msg, bool isCritical)
        {
            // Reset tray icon tooltip
            sysTrayIcon.Text = Assembly.GetExecutingAssembly().GetName().Name;

            ToolTipIcon icon = ToolTipIcon.None;

            // Set icon
            switch (status)
            {
                case WifiStatus.None:
                    sysTrayIcon.Icon = Resources.IconBlue16;
                    break;

                case WifiStatus.Authenticating:
                    sysTrayIcon.Icon = Resources.IconYellow16;
                    icon = ToolTipIcon.Info;
                    break;

                case WifiStatus.Success:
                    sysTrayIcon.Icon = Resources.IconGreen16;
                    icon = ToolTipIcon.Info;
                    break;

                case WifiStatus.Failure:
                    sysTrayIcon.Icon = Resources.IconRed16;
                    icon = ToolTipIcon.Error;
                    break;

                case WifiStatus.ConnectedToAnotherNetwork:
                    sysTrayIcon.Icon = Resources.IconBlue16;
                    break;

                default:
                    break;
            }

            // Display error
            // FIXME: this is seriously bad design. The GUINotificationManager should be
            // in charge of specifying error strings, not whoever generates the error.
            if (msg != null)
            {
                if ((Settings.Default.Balloons) || isCritical)
                {
                    // Display balloon
                    sysTrayIcon.ShowBalloonTip(5000, title, msg, icon);
                }

                // Set tray icon tooltip
                // (Tray icon text must be shorter than 64 characters)
                sysTrayIcon.Text = msg.Substring(0, msg.Length > 63 ? 63 : msg.Length);
            }
        }

        /// <summary>
        /// Start the notification manager
        /// </summary>
        public static void Start()
        {
            sysTrayIcon = new NotificationIcon().sysTrayIcon;
            WifiAuthManager.WifiAuthEvent += NotifyWifiAuthEventHandler;
        }
    }
}